/********************************************************************************
* Copyright (c) 2009, Matteo Pelati (matteo.pelati@gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Matteo Pelati ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**************************************************************************************/

#include "stdafx.h"
#include <iostream>
#include <exception>


#include "DbXmlQueryContext.h"
#include "DbException.h"
#include "DbXmlException.h"
#include "DbXmlValue.h"
#include "DbXmlResults.h"



namespace BerkeleyDB { namespace Xml
{

	void DbXmlQueryContext::InterruptQuery()
	{
		try
		{
			n_qctx->interruptQuery();
		}
		rethrowDbXmlException();
	}

	IDbXmlDatabaseItem^ DbXmlQueryContext::Get_Variables(String^ key)
	{
		try
		{
			CLRSTR2STDSTR(ckey, key);
			try
			{
				XmlValue outVal;
				if (!n_qctx->getVariableValue(ckey, outVal))
					return nullptr;
				return gcnew DbXmlValue(this, outVal, nullptr, nullptr);
			}
			catch(...)
			{	
				XmlResults outValSet;
				if (!n_qctx->getVariableValue(ckey, outValSet))
					return nullptr;
				return gcnew DbXmlResults(this, outValSet, nullptr, nullptr);		
			}
		}
		rethrowDbXmlException();
	}

	void DbXmlQueryContext::Set_Variables(String^ idx, IDbXmlDatabaseItem^ value)
	{
		try
		{
			CLRSTR2STDSTR(cidx, idx);
			DbXmlDatabaseItemType itmType = value->GetDatabaseItemType();

			if (itmType == DbXmlDatabaseItemType::XmlNode)
				n_qctx->setVariableValue(cidx, *(((DbXmlValue^)value)->GetInnerObject()));
			else if (itmType == DbXmlDatabaseItemType::XmlNodeSet || itmType == DbXmlDatabaseItemType::UnknownSet)
				n_qctx->setVariableValue(cidx, *(((DbXmlResults^)value)->GetInnerObject()));
			else
				throw new XmlException(XmlException::INVALID_VALUE, "Invalid object");
		}
		rethrowDbXmlException();
	}


	////////////////

	void DbXmlNamespaceCollection::Set(DbXmlNamespace^ ns)
	{
		try
		{
			CLRSTR2STDSTR(prefix, ns->Prefix);
			CLRSTR2STDSTR(uri, ns->Uri);
			n_qctx->setNamespace(prefix, uri);
		}
		rethrowDbXmlException();
	}

	void DbXmlNamespaceCollection::Remove(System::String ^prefix)
	{
		try
		{
			CLRSTR2STDSTR(cprefix, prefix);
			n_qctx->removeNamespace(cprefix);
		}
		rethrowDbXmlException();
	}

	void DbXmlNamespaceCollection::Clear()
	{
		try
		{
			n_qctx->clearNamespaces();
		}
		rethrowDbXmlException();
	}

	DbXmlNamespace^ DbXmlNamespaceCollection::Get(String^ prefix)
	{
		try
		{
			CLRSTR2STDSTR(cprefix, prefix);
			std::string uri = n_qctx->getNamespace(cprefix);
			return gcnew DbXmlNamespace(prefix, gcnew String(uri.c_str()));
		}
		rethrowDbXmlException();
	}

}}
